#ifndef RECORDINGWAVAUDIO_H
#define RECORDINGWAVAUDIO_H

#include <QObject>
#include <QThread>
#include <QDebug>

extern "C" {

    #include <libavdevice/avdevice.h>
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavutil/avutil.h>

}

struct WAVHead{
    uint8_t riffChunkId[4] = {'R','I','F','F'};
    // allSize - 8
    uint32_t riffChunkSize;
    uint8_t wave[4] = {'W','A','V','E'};
    uint8_t fmt[4] = {'f','m','t',' '};

    uint32_t fmtChunkSize = 16;

    /**
     * @brief dataType
     * 1: int
     * 3: float
     */
    uint16_t dataType = 1;
    // 声道数
    uint16_t numChannel;
    // 采用率
    uint32_t sampleRate;

    // perSecondByte sampleRate * blockAlign
    uint32_t byteRate;

    // sample Frame bytes  (bitsPerSample * numChannel) >> 3
    uint16_t blockAlign;
    // 位深度
    uint16_t bitsPerSample;

    uint8_t dataChunkId[4] = {'d','a','t','a'};
    // PCM 数据大小
    uint32_t dataSize;

    void init() {

        this->blockAlign = (bitsPerSample * numChannel) >> 3;
        this->byteRate = sampleRate * blockAlign;
    }
};

class RecordingWAVAudio : public QThread
{
    Q_OBJECT
public:
    explicit RecordingWAVAudio(QObject *parent = nullptr);
    ~RecordingWAVAudio();
    void recordingWAV();
signals:
    void timeCallBack(unsigned long long time);
private:
    void init();
    void run();

};

#endif // RECORDINGWAVAUDIO_H
